import { createApp } from './app'

export default context => {
    // 返回一个promise，服务器能够等待所有的内容在渲染前，已经准备就绪，
    return new Promise((resolve, reject) => {
        const { app, router, store } = createApp()
        
        router.push(context.url)

        router.onReady(() => {
            const matchedComponents = router.getMatchedComponents()
            if (!matchedComponents.length) {
                return reject({ code: 404 })
            }

            // 对所有匹配的路由组件调用 `asyncData()`
            Promise.all(matchedComponents.map(Component => {
                if (Component.asyncData) {
                    return Component.asyncData({
                        store,
                        route: router.currentRoute
                    })
                }
            })).then(() => {
                context.state = store.state

                resolve(app)
            }).catch(reject)
        }, reject)
    })
}
